package com.hzqy.service.activity;

import org.joda.time.DateTime;
import org.joda.time.Instant;

import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {
    private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 获得本周的第一天，周一
     *
     * @return
     */
    public static Date getCurrentWeekDayStartTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday);
            DateTime dateTime = new DateTime(new DateTime(c.getTime()).toString("yyyy-MM-dd") + " 00:00:00");
            dateTime.toString("yyyy-MM-dd HH:mm:ss");
            c.setTime(new Date(dateTime.toString("yyyy-MM-dd HH:mm:ss")));
          //  c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /**
     * 获得本周的最后一天，周日
     *
     * @return
     */
    public static Date getCurrentWeekDayEndTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            c.add(Calendar.DATE, 8 - weekday);
            DateTime dateTime = new DateTime(new DateTime(c.getTime()).toString("yyyy-MM-dd") + " 23:59:59");
            dateTime.toString("yyyy-MM-dd HH:mm:ss");
            c.setTime(new Date(dateTime.toString("yyyy-MM-dd HH:mm:ss")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    public static void main(String[] args) throws Exception {
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = getCurrentWeekDayStartTime();
        System.out.println("date1:"+longSdf.format(date));
        date = getCurrentWeekDayEndTime();
        System.out.println("date2:"+longSdf.format(date));
    }


}
